﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;
using PagedList;

namespace TaoNiuOA.Web.Controllers
{
    public class PerformanceController : Controller
    {
        private DbEntity db = new DbEntity();

        int pageSize = 10;

        [IsAdmin]
        public ViewResult Index(int? page)
        {

            ViewData["actionName"] = "Index";
            var index = (page ?? 1) - 1;
            var user = Session["users"] as TaoNiuOA.Models.User;

            var month = DateTime.Now.ToString("yyyy-MM");
            var list = new List<Performance>();

            var mod = Request.QueryString["mod"];
            ViewData["mod"] = mod;

            //根据条件查询
            if (!string.IsNullOrEmpty(mod) && mod.IndexOf("-") != -1)
            {
                list = (from n in db.Performances where n.Month.Contains(mod) select n).ToList();
            }
            else
            {
                switch (mod)
                {
                    //所有
                    case "all":
                        list = (from n in db.Performances select n).ToList();
                        break;
                    //上个月 
                    case "lastmonth":
                        month = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
                        list = (from n in db.Performances where n.Month.Contains(month)  select n).ToList();
                        break;
                    //默认,本月
                    default:
                        list = (from n in db.Performances where n.Month.Contains(month) select n).ToList();
                        break;
                }
            }

            //统计信息
            double totalPoint = 0;
            double payPoint = 0;
            foreach (var p in list)
            {
                totalPoint += p.TotalPoint;
                payPoint += p.PayPoint;
            }
            ViewData["statInfo"] = "统计:共" + list.Count + "条记录,应得绩效:" + totalPoint + ",实得绩效:" + payPoint;

            return View(list.ToPagedList(index, pageSize));
        }

        //[IsAdmin]
        //public ViewResult Details(int id)
        //{
        //    Performance performance = db.Performances.Find(id);
        //    return View(performance);
        //}

        //[IsAdmin]
        //public ActionResult Create()
        //{
        //    return View();
        //}

        //[IsAdmin]
        //[HttpPost]
        //public ActionResult Create(Performance performance)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Performances.Add(performance);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    return View(performance);
        //}

        //[IsAdmin]
        //public ActionResult Edit(int id)
        //{
        //    Performance performance = db.Performances.Find(id);
        //    return View(performance);
        //}

        //[IsAdmin]
        //[HttpPost]
        //public ActionResult Edit(Performance performance)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(performance).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View(performance);
        //}

        //[IsAdmin]
        //public ActionResult Delete(int id)
        //{
        //    Performance performance = db.Performances.Find(id);
        //    return View(performance);
        //}

        //[IsAdmin]
        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Performance performance = db.Performances.Find(id);
        //    db.Performances.Remove(performance);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}